# -*- coding: utf-8 -*-

from osv import fields,osv
from osv.orm import browse_record
import tools
from functools import partial
import pytz
import pooler
from tools.translate import _
from service import security
import netsvc
import datetime

class aas_generate_plan_lines(osv.TransientModel):
    
    _name = 'aas.generate.plan.line'
    _columns = {
        'plan_id': fields.many2one('aas.academic.plan', string='', required=True, readonly=True),
        'year_id':fields.many2one('aas.academic.year', string='Năm học', readonly=True),
        'week_state':fields.many2one('aas.academic.week.state', string='Trạng thái tuần học', required=True),
        'sessions': fields.many2many('aas.academic.session', 'generate_plan_lines_sessions', 'generate_id', 'session_id', string='Các khóa', required=True),
        'weeks': fields.many2many('aas.academic.week', 'generate_plan_lines_weeks', 'generate_id', 'week_id', string='Các tuần học', required=True),
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_generate_plan_lines, self).default_get(cr, uid, fields, context=context)
        plan_id = context.get('active_id', False)
        year_id = False
        if plan_id:
            plan_obj = self.pool.get('aas.academic.plan')
            obj = plan_obj.browse(cr, uid, plan_id, context=context)
            if obj.academic_year and obj.academic_year.id:
                year_id =  obj.academic_year.id
        res.update({'plan_id': plan_id, 'year_id': year_id})
        return res
    
    def generate(self, cr, uid, ids, context=False):
        obj = self.browse(cr, uid, ids[0], context=context)
        if obj:
            plan_line_obj = self.pool.get('aas.academic.plan.line')
            
            for week in obj.weeks:
                for session in obj.sessions:
                    plan_line_data = {
                        'academic_plan': obj.plan_id.id,
                        'academic_session': session.id,
                        'academic_week': week.id,
                        'academic_week_state': obj.week_state.id,
                    }
                    plan_line_obj.create(cr, uid, plan_line_data, context=context)
        return {}
    
aas_generate_plan_lines()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
